package com.xiaoyu.cloudmusic.controller;

import com.xiaoyu.cloudmusic.pojo.ReportDetail;
import com.xiaoyu.cloudmusic.pojo.Song;
import com.xiaoyu.cloudmusic.service.AdminService;
import com.xiaoyu.cloudmusic.service.PlayListService;
import com.xiaoyu.cloudmusic.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    PlayListService playListService;
    /**
     * admin账户登录
     */
    @RequestMapping("/admin/login")
    public Result adminLogin(String name,String password){
        int isLogin = adminService.login(name,password);
        if(isLogin == 1){
            return  Result.ok("欢迎登录，" + name + "!").put("isLogin",true);
        }
        return Result.error("用户名或密码错误！").put("isLogin",false);
    }

    /**
     * 获取数据库歌曲
     */
    @RequestMapping("/admin/paging")
    public Result paging(@RequestParam("pageNo")Integer pageNo,@RequestParam("pageSize")Integer pageSize){
        List<Song> songList = adminService.paging(pageNo * pageSize,pageSize);
        if(songList != null){
            return Result.ok().put("songList",songList);
        }
        return Result.error("请联系维护人员维护！");
    }
    /**
     * 下架歌曲
     */
    @RequestMapping("/admin/removeSongs")
    public Result removeSong(@RequestParam("ids") List<Integer> ids){
        int status = adminService.removeSongs(ids);
        if(status > 0){
            return Result.ok("移除成功！");
        }
        return Result.error("数据库错误，请联系维护人员维护！");
    }

    /**
     * 上架歌曲
     */
    @RequestMapping("/admin/shelvesSongs")
    public Result shelvesSongs(@RequestParam("ids") List<Integer> ids){
        int status = adminService.shelvesSongs(ids);
        if(status > 0){
            return Result.ok("上架成功！");
        }
        return Result.error("数据库错误，请联系维护人员维护！");
    }

    /**
     * 增加歌曲资源
     */
    @RequestMapping("/admin/addSongs")
    public Result addSongs(@RequestBody List<Song> songList){
        System.out.println(songList);
        if(adminService.addSongs(songList)){
            return Result.ok("添加成功！");
        }
        return Result.error("数据库插入错误，请联系维护人员维护！");
    }

    /**
     * 举报评论
     */
    @RequestMapping("/admin/reportComment")
    public Result reportComment(@RequestBody ReportDetail reportDetail){
         if(adminService.addReportInfo(reportDetail)){
             return Result.ok("举报成功!").put("report",true);
         }
         return Result.error("举报失败，请联系客服处理！").put("report",false);
    }
    @RequestMapping("/admin/getReportInfo")
    public Result getReportInfo(){
        List<ReportDetail> reportDetails = adminService.getReportInfo();
        return Result.ok().put("reportDetails",reportDetails);
    }
    @RequestMapping("/admin/delReportComment")
    public Result delReportComment(Integer id,Integer commentid){

        if(adminService.delReportComment(id,commentid)){
            return Result.ok("删除成功！");
        }
        return Result.error("请联络维修人员维护！");
    }
    @RequestMapping("/admin/revokeReport")
    public Result revokeReport(Integer id){
        int res = adminService.revokeReport(id);
        if(res == 1) return Result.ok("撤销成功！");
        return Result.error("撤销失败，请联系维修人员");
    }


    @RequestMapping("/admin/getWeeklyStyle")
    public Result getWeeklyStyle(){
        return Result.ok().put("styleCount",adminService.getStyleCounts());
    }

}
